package main

import (
	"fmt"
	"html/template"
	"net/http"
)

func process(w http.ResponseWriter, r *http.Request) {
	r.ParseForm() // 解析表单
	fmt.Println("method: ", r.Method)
	if r.Method == "GET" {
		//加载界面模板
		t, _ := template.ParseFiles("./view/login.ctpl")
		//将解析好的模板应用到data上，这里data为nil
		t.Execute(w, nil)
	} else if r.Method == "POST" {
		msg := r.FormValue("message")
		fmt.Println("msg: ", msg)
	}

	w.Write([]byte("ok!!!"))
	//fmt.Fprintln(w, r.Form)
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:9090",
	}
	http.HandleFunc("/req", process)
	server.ListenAndServe()
}

/*
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func process(w http.ResponseWriter, r *http.Request) {
	file, _, err := r.FormFile("uploaded")
	if err == nil {
		data, err := ioutil.ReadAll(file)
		if err == nil {
			fmt.Fprintln(w, string(data))
		}
		fmt.Println(string(data))
	}
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}
	http.HandleFunc("/process", process)
	server.ListenAndServe()
}
*/
